﻿using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class RC_PlacementByExam : System.Web.UI.Page
    {
        private SsMng ssMng = new SsMng();
        private ICommandBLL cmdBLL = new CommandBLL();
        private ICommandDetailBLL dtlsBLL = new CommandDetailBLL();
        private CommandInfo cmdInfo = new CommandInfo();
        protected void Page_Load(object sender, EventArgs e)
        {
            IncludeJava();

            if (!Page.IsPostBack)
            {
                lblCmdSeq.Text = Request.QueryString["CmdSeq"].ToString();

                IList<CommandInfo> infoList = cmdBLL.ListCommand(SystemInfo._CmdEmployee, Int32.Parse(lblCmdSeq.Text.Trim()), (int?)null, (int?)null);
                if (infoList.Count > 0)
                {
                    cmdInfo = infoList[0];

                    lblName.Text = cmdInfo.Name;// Request.QueryString["Name"].ToString();
                    lblRefNo.Text = cmdInfo.RefNo;// Request.QueryString["RefNo"].ToString();
                    lblApvDate.Text = string.Format("{0:dd/MM/yyyy}", cmdInfo.ApprovedDate);// Request.QueryString["ApvDate"].ToString();
                    lblEmpType.Text = cmdInfo.EmployeeType.Sequence.ToString();// Session["EMPTYPE"].ToString();
                    lblCmdType.Text = cmdInfo.CmdType.Sequence.ToString();
                }
                LoadDataGrid();
            }

        }
        public void IncludeJava()
        {
            HtmlGenericControl script = new HtmlGenericControl();
            script.TagName = "script";
            script.Attributes.Add("type", "text/javascript");
            script.Attributes.Add("src", "../../Scripts/jsfunction.js");
            Page.Header.Controls.Add(script);
        }
        public void LoadDataGrid()
        {         
            string CmdTypeSeq = lblCmdType.Text.Trim();
            switch (CmdTypeSeq)
            {
                case "101"://คำสั่งบรรจุผู้สอบแข่งขันได้
                    ViewState["ReportName"] = "RPT_CMD_1-1";
                    RadGrid1.MasterTableView.EditFormSettings.UserControlName = "../UserControls/RC_CmdPlacementExam.ascx";
                    break;
                case "102"://คำสั่งบรรจุผู้ได้รับคัดเลือก
                    ViewState["ReportName"] = "RPT_CMD_1-2";
                    RadGrid1.MasterTableView.EditFormSettings.UserControlName = "../UserControls/RC_PlacementBySelect.ascx";
                    break;
                case "301"://คำสั่งรับโอนข้าราชการพลเรือนสามัญ
                    RadGrid1.MasterTableView.EditFormSettings.UserControlName = "../UserControls/RC_Transfers.ascx";
                    break;
                case "302"://คำสั่งรับโอนข้าราชการพลเรือนสามัญผู้ได้รับวุฒิเพิ่มขึ้น
                    RadGrid1.MasterTableView.EditFormSettings.UserControlName = "../UserControls/RC_TransferQualOfficialPlus.ascx";
                    break;
                case "303"://คำสั่งรับโอนข้าราชการพลเรือนสามัญมาดำรงตำแหน่งในระดับที่สูงขึ้น
                    RadGrid1.MasterTableView.EditFormSettings.UserControlName = "../UserControls/RC_TransferOfficialPlus.ascx";
                    break;
            }

            //RC_PlacementByExamBLL obj = new RC_PlacementByExamBLL();
            DataTable dt = null;// obj.Select(Convert.ToInt32(lblCmdSeq.Text.Trim()));

            DataSet ds = dtlsBLL.LoadCommandDetails((int?)null, Convert.ToInt32(lblCmdSeq.Text.Trim()), (int?)null);
            if (ds.Tables.Count > 0)
                dt = ds.Tables[0];

            RadGrid1.DataSource = dt;
            RadGrid1.DataBind();
        }

        protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                int seq = Convert.ToInt32((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["cmd_dtls_seq"]);

                if (lblCmdType.Text.Trim() == "101")
                    dtlsBLL.Delete(seq, ssMng.EmpSeqI);
                else
                {
                    RC_PlacementByExamBLL obj = new RC_PlacementByExamBLL();
                    obj.Delete(seq, ssMng.EmpSeqI);
                }
            }
            else if (e.CommandName == "btnClick")
            {
                return;
            }
            else if (e.CommandName == "PrintPreview")
            {
                DataTable dt = null;
                DataSet ds = dtlsBLL.LoadCommandDetails((int?)null, Convert.ToInt32(lblCmdSeq.Text.Trim()), (int?)null);
                if (ds.Tables.Count > 0)
                    dt = ds.Tables[0];

                Session["dtReport"] = dt;
                ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "onclick", "openReport('" + ViewState["ReportName"] + "')", true);
                return;
            }
            Session["CMD"] = lblCmdSeq.Text.Trim();
            Session["EMPTYPE"] = lblEmpType.Text.Trim();
            LoadDataGrid();
        }

    }
}
